
//
//  ChouseImageVC.m
//  LyhToolsTest
//
//  Created by daojial on 2019/8/23.
//  Copyright © 2019 LYH.com. All rights reserved.
//

#import "ChooseImageVC.h"
#import "WSLWaterFlowLayout.h"
@interface ChooseImageVC ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,WSLWaterFlowLayoutDelegate>
{
      WSLWaterFlowLayout * _flow;
}
@property (nonatomic, strong) UITableView *leftTableview;
@property (nonatomic, strong) UICollectionView *rightCollectionView;

@property (nonatomic, strong) NSMutableArray *titleArray;

@end

@implementation ChooseImageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
}
- (void)zj_viewDidLoadForIndex:(NSInteger)index {
    NSLog(@"--index--%ld", index);
    
    [self leftTableview];
    [self rightCollectionView];
}
- (void)zj_viewWillAppearForIndex:(NSInteger)index
{
   
}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建cell
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font=[UIFont systemFontOfSize:14];
        cell.textLabel.textColor = [UIColor darkTextColor];
    }
    
    // 2.设置cell的数据
    cell.textLabel.text = self.titleArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1.0f;
}


#pragma -mark ---------------------collectionView的代理和数据源方法---------------------------------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YHImageCell *cell = [YHImageCell codeCell:collectionView cellForItemAtIndexPath:indexPath];
    cell.imageViewitem.image = [UIImage imageNamed:@"img1"];
    cell.imageViewitem.contentMode = UIViewContentModeScaleAspectFill;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

#pragma mark - WSLWaterFlowLayoutDelegate
//返回每个item大小
- (CGSize)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    CGFloat itemWidth = (SCREEN_WIDTH - (110*WpercentOfSeven)-45)/2;
   

//    if (((indexPath.row +3)/4) % 2 ==0) {
//         self.tagIndex = 2;
//    }else{
//         self.tagIndex = 1;
//    }
    
    if ((indexPath.row % 2) == 0) {
        if (((indexPath.row +3)/4) % 2 ==0) {//高
            return CGSizeMake(itemWidth, 220 *WpercentOfSeven);
        }else{//短
           return CGSizeMake(itemWidth, 200 *WpercentOfSeven);
        }
       
    }else{
        if (((indexPath.row +3)/4) % 2 ==0) {//短
           return CGSizeMake(itemWidth, 200 *WpercentOfSeven);
        }else{//高
            return CGSizeMake(itemWidth, 220 *WpercentOfSeven);
        }
    }
}
/** 列数*/
-(CGFloat)columnCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout{
    return 2;
}
/** 列间距*/
-(CGFloat)columnMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout{
    return 15;
}
/** 行间距*/
-(CGFloat)rowMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout{
    return 15;
}
/** 边缘之间的间距*/
-(UIEdgeInsets)edgeInsetInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout{
    
   
    return UIEdgeInsetsMake(15, 15, 15, 15);
}

//懒加载tableview
- (UITableView *)leftTableview{
    if(!_leftTableview){
        //布局比例

        _leftTableview=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 110*WpercentOfSeven, self.view.height) style:UITableViewStyleGrouped];
        if (@available(iOS 11.0, *)) {
            _leftTableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _leftTableview.estimatedSectionHeaderHeight = 0;
            _leftTableview.estimatedSectionFooterHeight = 0;
        }
        //竖直滚动条不显示
        _leftTableview.showsVerticalScrollIndicator=NO;
        //iOS 8开始的自适应高度，可以不需要实现定义高度的方法
        _leftTableview.estimatedRowHeight = 0;
        //_tableView.rowHeight = UITableViewAutomaticDimension;
        _leftTableview.rowHeight=50;
        _leftTableview.dataSource=self;
        _leftTableview.delegate=self;
        _leftTableview.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
        //设置分割线铺满cell
        [_leftTableview setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        _leftTableview.separatorColor = [UIColor grayColor];
        [self.view addSubview:_leftTableview];
    }
    return _leftTableview;
}

//懒加载collectionview
- (UICollectionView *)rightCollectionView
{
    if (!_rightCollectionView) {
        _flow = [[WSLWaterFlowLayout alloc] init];
        _flow.delegate = self;
        _flow.flowLayoutStyle = WSLWaterFlowVerticalEqualWidth;
        
        _rightCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(110*WpercentOfSeven, 0, SCREEN_WIDTH-110*WpercentOfSeven, self.view.height) collectionViewLayout:_flow];
        _rightCollectionView.dataSource=self;
        _rightCollectionView.delegate=self;
        _rightCollectionView.backgroundColor=[UIColor whiteColor];
        //支持分页
        _rightCollectionView.pagingEnabled=NO;
        _rightCollectionView.scrollEnabled=YES;
        _rightCollectionView.alwaysBounceVertical = YES;//数据少时也能滚动
        [self.view addSubview:_rightCollectionView];
        //[collectionView registerNib:[UINib nibWithNibName:@"GWRandomPracticeTwoCell" bundle:nil] forCellWithReuseIdentifier:GWRandomPracticeTwoCellId];
    }
    return _rightCollectionView;
}
- (NSMutableArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = [NSMutableArray arrayWithArray:@[@"全部",@"分类1",@"分类2",@"分类3",@"分类4",@"分类5"]];
    }
    return _titleArray;
}
@end
